<?php
class TrainingService {
    private static $instance;

    public static function get() {
        if (!self::$instance) {
            self::$instance = new TrainingService();
        }
        return self::$instance;
    }

    protected function __construct() {

    }

    /**
     * 初始化数据
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function info($userId) {
        $trainingStartTime = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'trainingStartTime', true);
        $trainingStartTime = $trainingStartTime['value'];
        $registerTime = UserService::get()->getUserRegisterTime($userId);
        if ($registerTime < $trainingStartTime) {
            return array(

            );
        }

        $trainingInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRAINING_INFO);
        $trainingSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_TRAINING, null, true,false);
        if (!$trainingSettings) {
            return array(
                'trainingInfoList' => $trainingInfoList->toArray()
            );
        }
        $isAdd = false;
        foreach ($trainingSettings as $trainingSetting) {
            $trainingInfo = $trainingInfoList->getElement($trainingSetting['id'],false);
            if (!$trainingInfo) {
                $isAdd = true;
                $trainingInfo = new TrainingInfoVo($userId,$trainingSetting['id'],0,0,true);
                $trainingInfoList->addElement($trainingInfo);
                $trainingInfoList->setElement($trainingSetting['id'],$trainingInfo);
            }
        }

        if ($isAdd) {
            SzPersister::get()->setVoList($trainingInfoList);
        }
        return array(
            'trainingInfoList' => $trainingInfoList->toArray()
        );
    }

    /**
     * 领取奖励
     * @param $userId
     * @param $id
     * @return bool
     * @throws SzException
     */
    public function reward($userId,$id) {
        $trainingSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_TRAINING, $id, true);
        /**
         * @var $trainingInfoList TrainingInfoVoList
         * @var $trainingInfo TrainingInfoVo
         */
        $trainingInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRAINING_INFO);
        $trainingInfo = $trainingInfoList->getElement($id);
        $progress = $trainingInfo->getProgress();
        if ($progress < $trainingSetting['progress']) {
            throw new SzException(90051);
        }

        if ($trainingInfo->getStatus()) {
            throw new SzException(90007);
        }

        $output = '';

        //获取玩家平台
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        if ($platform == 'tt'){
            foreach ($trainingSetting['tiktokReward'] as $entryId=>$count) {
                GameService::get()->handleEntity($userId, $entryId, $count, true, true);
                $output .= $entryId.':'.$count.'|';
            }
        }
        else{
            foreach ($trainingSetting['reward'] as $entryId=>$count) {
                GameService::get()->handleEntity($userId, $entryId, $count, true, true);
                $output .= $entryId.':'.$count.'|';
            }
        }

        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_TRAINING,$userId,1,$output,'reward');//获得资源上报

        $trainingInfo->setStatus(1);
        $trainingInfoList->updateElement($id,$trainingInfo);
        SzPersister::get()->setVoList($trainingInfoList);

        return true;
    }

    /**
     * @param $userId
     * @param $type
     * @param $number
     * @throws SzException
     */
    public function updateProgress($userId,$type,$number) {
        $trainingSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_TRAINING, null, true,false);
        if (!$trainingSettings) {
            return;
        }

        $userLevel = UserService::get()->getUserLevel($userId);
        $trainingSetting = array();
        foreach ($trainingSettings as $val) {
            if ($val['type'] == $type) {
                $trainingSetting = $val;
                break;
            }
        }
        if (!$trainingSetting || $trainingSetting['level'] > $userLevel) {
            return;
        }

        $lastId = $trainingSetting['lastId'];
        /**
         * @var $trainingInfoList TrainingInfoVoList
         * @var $trainingInfo TrainingInfoVo
         * @var $lastTrainingInfo TrainingInfoVo
         * @var $nextTrainingInfo TrainingInfoVo
         */
        $trainingInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_TRAINING_INFO);
        $trainingInfo = $trainingInfoList->getElement($trainingSetting['id'],false);
        if (!$trainingInfo) {
            return;
        }

        $progress = $trainingInfo->getProgress();
        if ($progress >= $trainingSetting['progress']) {
            return;
        }

        if ($lastId) {
            $lastTrainingInfo = $trainingInfoList->getElement($lastId,false);
            if (!$lastTrainingInfo) {
                return;
            }
            if (!isset($trainingSettings[$lastId])) {
                return;
            }
            $lastTrainingSetting = $trainingSettings[$lastId];
            $lastProgress = $lastTrainingInfo->getProgress();
            if ($lastProgress < $lastTrainingSetting['progress']) {
                return;
            }
        }

        $progress = $progress + $number;
        $trainingInfo->setProgress($progress);
        $trainingInfoList->updateElement($trainingSetting['id'],$trainingInfo);

        if($type == 6 && $progress >= $trainingInfo->getProgress()) {
            $nextId = $trainingSetting['nextId'];
            $nextTrainingInfo = $trainingInfoList->getElement($nextId,false);
            if ($nextTrainingInfo) {
                $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
                $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
                if ($guildId) {
                    $nextTrainingInfo->setProgress(1);
                    $trainingInfoList->updateElement($nextId,$nextTrainingInfo);
                }
            }
        }

        SzPersister::get()->setVoList($trainingInfoList);
        return;
    }
}
